var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;
    $.removeTableCookie('SpryMedia_DataTables_companyTable_companyList.jsp');
    getDataTable();
    //新增
    $("#addCompany").click(function() {
        saveOrUpdateCom(null);
    });

    // 查询
    $("#search").click(function () {
        $.removeTableCookie('SpryMedia_DataTables_companyTable_companyList.jsp');
        getDataTable(); //要触发的方法
        return false;
    });

    //回车事件
    $(document).keydown(function (event) {
        var code = event.which;
        if (code == 13) {
            $.removeTableCookie('SpryMedia_DataTables_companyTable_companyList.jsp');
            getDataTable(); //要触发的方法
            return false;
        }
    });
});

/**
 * 获取列表
 */
function getDataTable() {
    $('#companyTable').dataTable({
        'bProcessing' : true,
        'bServerSide' : true,
        'fnServerParams' : function(aoData) {
            var companyName = $.trim($("#companyName").val());
            if (companyName == "") {
                companyName = null;
            }
            aoData.push({
                "name" : "companyName",
                "value" : companyName
            });
        },
        "sAjaxSource" : basePath + "company/getCompanyList.action", // 获取列表
        "sServerMethod" : "POST",
        "sPaginationType" : "full_numbers",
        "bPaginate" : true, // 翻页功能
        "bStateSave" : true, // 状态保存
        "bLengthChange" : false, // 改变每页显示数据数量
        "bFilter" : false, // 过滤功能
        "bSort" : false, // 排序功能
        "bInfo" : true, // 页脚信息
        "bAutoWidth" : false, // 自动宽度
        "bDestroy" : true,
        "iDisplayLength" : 15, // 每页显示多少行
        "aoColumns" : [ {
            "sTitle" : '排序',
            "mDataProp" : "no"
        }, {
            "sTitle" : '公司名称',
            "mDataProp" : "companyName",
            "sClass" : "conceal"
        }, {
            "sTitle" : '地址',
            "mDataProp" : "address",
            "sClass" : "conceal"
        }, {
            "sTitle" : '联系人',
            "mDataProp" : "contacts"
        }, {
            "sTitle" : '手机',
            "mDataProp" : "cellphone"
        }, {
            "sTitle" : '电话',
            "mDataProp" : "telephone"
        }, {
            "sTitle" : '传真',
            "mDataProp" : "fax"
        }, {
            "sTitle" : '邮箱',
            "mDataProp" : "email"
        }, {
            "sTitle" : '创建日期',
            "mDataProp" : "createTime"
        }, {
            "sTitle" : '操作',
            "mDataProp" : null,
            "sClass" : "right_bdr0"
        } ],
        "oLanguage" : {
            "sUrl" : basePath + "plugins/datatable/cn.txt" // 中文包
        },
        "fnDrawCallback" : function(oSettings) {
            $('#companyTable tbody  tr td').each(function() {
                this.setAttribute('title', $(this).text());
            });
        },
        "fnInitComplete" : function() {},
        "aoColumnDefs" : [ {
            "aTargets" : [ 1 ],
            "fnRender" : function(oObj) {
                var companyId = oObj.aData.id;
                var companyName = oObj.aData.companyName;
                return '<a href="javascript:void(0);" onclick=detailCompany("' + companyId + '")>' + companyName + '</a>';
            }
        }, {
            "aTargets" : [ 9 ],
            "fnRender" : function(oObj) {
                var companyId = oObj.aData.id;
                return "<a data-method=\"repaire\" class=\"layui-btn layui-btn-small layui-btn-normal layui-btn-radius\" onclick=saveOrUpdateCom(\"" + companyId + '")>修改</a>'
                    + "<a data-method=\"del\" class=\"layui-btn layui-btn-small layui-btn-danger layui-btn-radius\" onclick=deleteCompany(\"" + companyId + '")>删除</a>';
            }
        } ]
    });
}
/**
 * 新增/修改
 */
function saveOrUpdateCom(companyId) {
    var url;
    var titleVal;
    if (null == companyId) {
        url = "jsp/company/addOrUpdateCompany.jsp";
        titleVal = "新增公司信息";
    } else {
        url = "company/toUpdateOrDetail.action?type=update&id=" + companyId;
        titleVal = "修改公司信息";
    }
    layer.open({
        type: 2,
        title: titleVal,
        area: ["50%", "59%"],
        shade: 0.3,
        maxmin: true,
        content: basePath + url,
        zIndex: layer.zIndex,
        success: function (layero) {//弹出后回调
            layer.setTop(layero); //弹窗置于最上层
        }
    });
}

/**
 * 查看详情页面
 */
function detailCompany(companyId) {
    var url = "company/toUpdateOrDetail.action?type=detail&id=" + companyId;
    layer.open({
        type: 2,
        title: '查看公司信息',
        area: ["50%", "60%"],
        shade: 0.3,
        maxmin: true,
        content: basePath + url,
        btn: ['关闭'],
        yes: function(index){
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },
        zIndex: layer.zIndex,
        success: function (layero) {
            layer.setTop(layero); //弹窗置于最上层
        }
    });
}

/**
 * 删除
 */
function deleteCompany(companyId) {
    layer.confirm('确定删除此条数据?', {
        title: " 确认",
        icon: 3,
        skin: 'layui-layer-molv',
        btn: ["确定", "取消"],
        yes: function (index) {
            $.ajax({
                url: basePath + "company/deleteCompany.action?id=" + companyId,
                type: "post",
                dataType: 'text',
                success: function (data) {
                    if (data != null && data != "" && data == 0) {
                        getDataTable();
                    } else if (data == 1) {
                        layer.open({
                            title: '提示',
                            icon: 0,
                            content: '该公司下存在部门，需先删除部门！'
                        });
                        getDataTable();
                    } else {
                        layer.open({
                            title: '错误',
                            icon: 2,
                            content: '删除失败！'
                        });
                        getDataTable();
                    }
                }
            });
            layer.close(index);
        }
    });
}